<template>
  <div class="login">
    <div id="logoimg">
      <img src="../assets/images/logo copy.png" alt />
      <p>一年四季，一日三餐</p>
    </div>
    <div id="myregister">
      <div id="registertitle">用户登录</div>
      <el-form
        :model="loginUser"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="loginForm"
      >
        <el-form-item label="手机号" prop="phone" ref="tel">
          <el-input v-model="loginUser.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userPassword">
          <el-input type="password" v-model="loginUser.userPassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" id="btnLogin">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <el-form-item label=""><router-link to='/register' id='registertext'>还没注册-点击注册</router-link><router-link to='/index/home' id='registertext'>返回首页</router-link></el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import jwt_decode from 'jwt-decode'
export default {
  name: "login",
  data: function() {
    return {
      loginUser: {
        phone: "",
        userPassword: ""
      },
      rules: {
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          { min: 11, max: 11, message: "手机号码必须是11位", trigger: "blur" }
        ],
        userPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 6, message: "密码必须是6位", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      //通过ref定位到form表单
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("确定提交吗？");
          this.$axios
            .post("http://localhost:3000/users/login", this.loginUser)
            .then(res => {
              if(res.data.msg=='手机号不存在，登录失败！'){
                alert(res.data.msg)
              }else if(res.data.msg=='密码错误登录失败! '){
                alert(res.data.msg)
              }else if(res.data.msg=='登录成功！'){
                // alert(res.data.msg)
              }
              console.log("登录成功！", res.data.msg);
              console.log(res.data.token)
              console.log('token对象：',jwt_decode(res.data.token))
              localStorage.setItem('mytoken',res.data.token)  //1.把token保存到本地存储
              this.$router.push("/index/home"); //路由转向登录组件
            })
            .catch(err => {
              console.log(err);
            });
        } else {
          console.log("提交错误!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      //实现重置表单元素数据
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style lang="css" scoped>
.el-input {
  width: 250px;
}
* {
  padding: 0px;
  padding: 0px;
}

#logoimg {
  width: 300px;
  height: 300px;
  margin: -69px;
}
#logoimg p {
  position: relative;
  top: -60px;
  left: 81px;
  color: rgb(64, 116, 52);
}
.login{
  min-width: 850px;
  min-height: 600px;
  width: 100%;
  height: 120%;
  background: linear-gradient(45deg, rgb(160, 191, 124), rgb(255, 255, 255))
    fixed;
}
.loginForm {
  padding-top: 60px;
  width: 400px;
  height: 350px;
  border: 1px solid rgb(160, 191, 124);
  border-radius: 10px;
  position: absolute;
  top: 150px;
  left: 60%;
  background-color: white;
  color: rgb(64, 116, 52);
}
#registertitle {
  position: absolute;
  top: 15%;
  left: 71%;
  font-size: 25px;
  font-weight: bolder;
  color:#407434;
}
#btnLogin {
  /* margin: 20px 60px 0; */
  margin-left: 30px;
  border: none;
  outline: none;
  width: 70px;
  height: 38px;
  line-height: 10px;
  border-radius: 5px;
  background: linear-gradient(rgb(160, 191, 124), rgb(101, 147, 74));
  box-shadow: 0 0 3px rgb(64, 116, 52);
  cursor: pointer;
  color: white;
}
#registertext{
  margin-left: 40px;
}
</style>